package cc.lj.internal.payment.infrastructure.repository.mysql;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.model.BasePageHelper;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.payment.domain.pay.repository.ITicketPayRecordRepository;
import cc.lj.internal.payment.infrastructure.dao.ITicketPayRecordMapper;
import cc.lj.internal.payment.infrastructure.exception.PaymentExceptionEnum;
import cc.lj.internal.payment.infrastructure.po.TicketPayRecordPO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * 合同收券表　mysql仓储实现
 * @author liangxifeng
 * @date 2022-01-12
 */
@Repository("ticketPayRecordMysqlRespository")
public class TicketPayRecordMysqlRespository implements ITicketPayRecordRepository,BasePageHelper<TicketPayRecordPO> {
    @Autowired
    private ITicketPayRecordMapper mapper;
    @Override
    public List list(TicketPayRecordPO entityPO) {
        QueryWrapper<TicketPayRecordPO> wrapper = new QueryWrapper<>(entityPO);
        List<TicketPayRecordPO> entitys = mapper.selectList(wrapper);
        if(entitys == null)
        {
            throw new ServiceException(PaymentExceptionEnum.TICKET_PAY_RECORD_LIST_IS_NULL);
        }
        return entitys;
    }

    @Override
    public Integer updateById(TicketPayRecordPO entityPO) {
        Integer res = mapper.updateById(entityPO);
        if (res != 1)
        {
            throw new ServiceException(PaymentExceptionEnum.UPDATE_TICKET_PAY_RECORD_FAILED);
        }
        return res;
    }

    @Override
    public PageInfo<TicketPayRecordPO> selectPage(PageParam pageParam) {
        return this.page(pageParam);
    }


    @Override
    public List<TicketPayRecordPO> getList(TicketPayRecordPO TicketPayRecordPO) {
        return this.list(TicketPayRecordPO);
    }

    @Override
    public Integer insert(TicketPayRecordPO ticketPayRecordPO) {
        Integer rows = mapper.insert(ticketPayRecordPO);
        if(rows != 1) {
            throw new ServiceException(PaymentExceptionEnum.INSERT_TICKT_PAY_RECORD_FAILED);
        }
        return ticketPayRecordPO.getId();
    }

    @Override
    public BigDecimal getMartSharePriceSum(TicketPayRecordPO ticketPayRecordPO) {
        LambdaQueryWrapper<TicketPayRecordPO> query = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<TicketPayRecordPO> wrapper = new LambdaQueryWrapper();
        //商户展位号
        if(ticketPayRecordPO.getMerchantShow() != null ) {
            wrapper.eq(TicketPayRecordPO::getMerchantShow, ticketPayRecordPO.getMerchantShow());
        }
        //封顶额活动主键
        if(ticketPayRecordPO.getCapId() != null) {
            wrapper.eq(TicketPayRecordPO::getCapId, ticketPayRecordPO.getCapId());
        }
        //电子券批次主键
        if(ticketPayRecordPO.getTicketBatchId() != null) {
            wrapper.eq(TicketPayRecordPO::getTicketBatchId, ticketPayRecordPO.getTicketBatchId());
        }
        BigDecimal sum = mapper.martSharePriceSum(wrapper) ;
        return sum == null ? new BigDecimal(0) : sum;
    }

}
